# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: 1.3 保留最后 N 个元素.py
@time: 2021/01/13
"""
from collections import deque


def search(lines, patter, pre_len=5):
    ...
    pre_lines = deque(maxlen=pre_len)
    for line in lines:
        if patter in line:
            yield line, pre_lines
        pre_lines.append(line)


def read(path):
    with open(path, mode='rt', encoding='utf-8') as f:
        for line, pre_lines in search(f, 'tgy'):
            ...
            print(f'{line}:pre_line', end='')
            for pre_line in pre_lines:

                print(pre_line, end='')

            print('*'*20)


if __name__ == '__main__':
    pass
    # read(r'/Users/tgy/Documents/tmp/demo01.txt')
    dq = deque(maxlen=2)
    dq.append(1)
    dq.append(2)
    dq.append(3)
    print(dq)
